package cn.hx.mall.goods.controller;

import cn.hx.mall.goods.entity.Brand;
import cn.hx.mall.goods.service.BrandService;
import cn.hx.mall.sell.entity.SmsHomeBrand;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@MultipartConfig
@Controller
@RequestMapping("goods")

public class ProductBrandController {
    List<Brand> brandList = new ArrayList<>();
    Brand brand = new Brand();
    @Resource
    private BrandService brandService;

    @GetMapping("productBrand")
    public String productBrand(Model model) {
        Page<Brand> info = PageHelper.startPage(1, 10);
        brandList = brandService.queryAll(null);
        PageInfo<Brand> pageInfo = new PageInfo<>(info);
        model.addAttribute("brandList", pageInfo);
        return "goods/brand";
    }

    @GetMapping("/brand")
    public String brandPage(Model model, Brand brand1,
                            @RequestParam(value = "page",defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<Brand> info = PageHelper.startPage(page, size);
        brandService.queryAll(brand1);
        PageInfo<Brand> pageInfo = new PageInfo<>(info);
        model.addAttribute("brandList", pageInfo);
        return "goods/brand";
    }
    @PostMapping("/selectBrand")
    public String brandList(Model model,
                            @RequestParam(value = "name") String name,
                            @RequestParam(value = "firstLetter") String firstLetter) {
        Page<Brand> info = PageHelper.startPage(1, 10);
        brandService.queryAllLike(name,firstLetter);
        PageInfo<Brand> pageInfo = new PageInfo<>(info);
        model.addAttribute("brandList", pageInfo);
        return "goods/brand";
    }

    @GetMapping("updBrand/{id}")
    public String updBrand(@PathVariable("id") Long id, Model model) {
//        brandList.forEach(o -> {
//            if (o.getId().equals(id)) {
//                brand = o;
//                System.out.println(brand.getId());
//                return;
//            }
//        });
//        System.out.println(brand.getId());
        brand = brandService.queryById(id);
        model.addAttribute("brand", brand);
        return "goods/brandPage";
    }
//    @GetMapping("brandPage")
//    public String brandPage() {
//        return "goods/brandPage";
//    }

    @PostMapping("updBrand")
    public String updBrand2(Brand brand1, MultipartFile file1,MultipartFile file2) throws IOException {
        //文件上传类路径下的地址
        String path = ResourceUtils.getURL("classpath:").getPath()+"static/upload";
        String realPath = path.replace('/', '\\').substring(1,path.length());
        //查看路径是否正确
        System.out.println(realPath);

        if(!file1.getOriginalFilename().isEmpty()){
            //获取文件的加密名称
            final String fileName = UUID.randomUUID()+file1.getOriginalFilename();
            //限制文件上传的类型
            File logo = new File(realPath,fileName);
            String path1 = "../upload/"+fileName;
            file1.transferTo(logo);
            brand1.setLogo(path1);
        }
        if(!file2.getOriginalFilename().isEmpty()){
            final String fileName2 = UUID.randomUUID()+file2.getOriginalFilename();
            File bigPic = new File(realPath,fileName2);
            String path2 = "../upload/"+fileName2;
            file2.transferTo(bigPic);
            brand1.setBigPic(path2);
        }
        brandService.update(brand1);
        return "redirect:/goods/productBrand";
    }


    @GetMapping("updateStatus1/{id}")
    public String updateStatus1(@PathVariable("id") Long id) {
        System.out.println(id);
        System.out.println(brandList);
        for (Brand o : brandList) {
            System.out.println(o.getId());
            if (o.getId() == id) {
                brand = o;
                break;
            }
        }
        ;
        System.out.println(brand.getFactoryStatus());
        if (brand.getFactoryStatus() == 1)
            brand.setFactoryStatus(0);
        else brand.setFactoryStatus(1);
        brandService.update(brand);
        return "goods/brand";
    }

    @GetMapping("updateStatus2/{id}")
    public String updateStatus2(@PathVariable("id") Long id) {
        brand = brandService.queryById(id);
        System.out.println(brand);
        if (brand.getShowStatus() == 1)
            brand.setShowStatus(0);
        else brand.setShowStatus(1);
        brandService.update(brand);
        return "goods/brand";
    }


    @GetMapping("delBrand/{id}")
    public String delBrand(@PathVariable("id") Long id) {
        brandService.deleteById(id);
        return "redirect:/goods/productBrand";
    }

    @RequestMapping("addBrand")
    public String addBrand() {
        return "goods/brandPage";
    }

    @PostMapping("addBrand")
    public String addBrand2(Brand brand1, MultipartFile file1, MultipartFile file2) throws IOException {

        //文件上传的地址
        String path = ResourceUtils.getURL("classpath:").getPath()+"static/upload";
        String realPath = path.replace('/', '\\').substring(1,path.length());
        //查看路径是否正确
        System.out.println(realPath);

        if(!file1.getOriginalFilename().isEmpty()){
            //获取文件的加密名称
            final String fileName = UUID.randomUUID()+file1.getOriginalFilename();
            //限制文件上传的类型
            File logo = new File(realPath,fileName);
            String path1 = "../upload/"+fileName;
            file1.transferTo(logo);
            brand1.setLogo(path1);
        }
        if(!file2.getOriginalFilename().isEmpty()){
            final String fileName2 = UUID.randomUUID()+file2.getOriginalFilename();
            File bigPic = new File(realPath,fileName2);
            String path2 = "../upload/"+fileName2;
            file2.transferTo(bigPic);
            brand1.setBigPic(path2);
        }
        brandService.insert(brand1);
        return "redirect:/goods/productBrand";
    }

//    @GetMapping("searchBrand")
//    public String searchBrand(String str,Model model;){
//        List<Brand> list = new ArrayList<>();
//        list = brandService.queryLike(str);
//        if (list!=null) {
//            model.addAttribute("brandList",brandList);
//        }
//        return "redirect:/goods/brand";
//    }
}
